package com.theexceptions.todo.controller;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;
import com.theexceptions.todo.model.DatabaseHelper;
import com.theexceptions.todo.model.IPersister;
import com.theexceptions.todo.model.Location;
import com.theexceptions.todo.view.LocationItemizedOverlay;
import com.theexceptions.todo.view.R;
import com.theexceptions.todo.view.SelectLocationItemizedOverlay;

/**
 * Controller that manages the functionality of the view for searching and
 * setting locations to a task.
 * 
 * @author Justian Meyer
 */
public class SearchMapController extends Controller {

	/**
	 * Returns an overlay consisting of a single newly searched location with an
	 * icon differing from the overlay consisting of all locations. That way, a
	 * user can select an existing location when connecting a location to a
	 * task.
	 * 
	 * @param address Address name
	 * @param point Location point
	 * @param context Activity context
	 * @return LocationItemizedOverlay
	 */
	public static LocationItemizedOverlay getNewLocationItemizedOverlay(String address, GeoPoint point,
			Activity activity) {
		Drawable drawable = activity.getResources().getDrawable(R.drawable.marker2);
		SelectLocationItemizedOverlay itemizedOverlay = new SelectLocationItemizedOverlay(drawable, activity);

		int lat = point.getLatitudeE6();
		int lon = point.getLongitudeE6();

		OverlayItem overlayitem = new OverlayItem(point, address, (-1) + " " + lat + " " + lon);
		itemizedOverlay.addOverlay(overlayitem);

		return itemizedOverlay;
	}

	/**
	 * Returns an overlay consisting of all locations associated with the user
	 * and their appropriate coordinates/titles. This way the user can use
	 * already existing locations when making a selection.
	 * 
	 * @param context Activity context
	 * @return LocationItemizedOverlay
	 */
	public static LocationItemizedOverlay getAllLocationsItemizedOverlay(Activity activity) {
		IPersister dh = new DatabaseHelper(context);
		List<Location> locations = dh.retrieveLocations(currentUser.getUserID());

		if (locations.size() <= 0)
			return null;

		Drawable drawable = context.getResources().getDrawable(R.drawable.marker);
		SelectLocationItemizedOverlay itemizedOverlay = new SelectLocationItemizedOverlay(drawable, activity);

		for (Location loc : locations) {
			long id = loc.getID();
			String name = loc.getName();
			int lat = loc.getLatitude();
			int lon = loc.getLongitude();

			GeoPoint point = new GeoPoint(lat, lon);
			OverlayItem overlayitem = new OverlayItem(point, name, id + " " + lat + " " + lon);

			itemizedOverlay.addOverlay(overlayitem);
		}

		return itemizedOverlay;
	}

	/**
	 * Sets the location being used in AddTaskController.
	 * 
	 * @param loc
	 */
	public static void setLocation(Location loc) {
		AddTaskController.setLocation(loc);
		System.out.println(loc);
	}

}
